package cn.xtits.design.config;

import cn.xtits.design.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @author Dan
 * @date 2017/10/26
 */
@Configuration
public class WebResourceAppConfig {

    private static final Logger logger = LoggerFactory.getLogger(WebResourceAppConfig.class);

    /**
     * 文件根目录-必须存在,如:(windows:[P:/],linux:[/home/])
     */
    @Value("${pldesign.upload.printerRootUrl}")
    private String printerRootUrl;

    /**
     * 需要创建的目录-如：(dan/file), 文件的全路径就是:rootPath + filePath
     */
    @Value("${pldesign.upload.printerFileUrl}")
    private String printerFileUrl;

    /**
     * 默认：[file:]
     */
    @Value("${pldesign.upload.path}")
    private String path;

    /**
     * 相对路径:[staticPath+文件名称]
     */
    @Value("${pldesign.upload.printerStaticUrl}")
    private String printerStaticUrl;

    public ResourceConfig getResourceConfig() {
        //文件路径
        String fileUrl = getFileUrl();
        logger.info("fileUrl=" + fileUrl);
        logger.info("path=" + path);
        return new ResourceConfig(printerStaticUrl + "/**", path + fileUrl);
    }

    /**
     * 文件全路径(不加上文件名称)
     */
    public String getFileUrl() {
        return FileUtil.createMkdirMulti(printerRootUrl, true, printerFileUrl);
    }

    public String getRootPath() {
        return printerRootUrl;
    }

    public String getFilePath() {
        return printerFileUrl;
    }

    public String getPath() {
        return path;
    }

    public String getStaticPath() {
        return printerStaticUrl;
    }

    public class ResourceConfig {
        private String path;
        private String resourceLocations;

        public ResourceConfig() {
        }

        public ResourceConfig(String path, String resourceLocations) {
            this.path = path;
            this.resourceLocations = resourceLocations;
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        public String getResourceLocations() {
            return resourceLocations;
        }

        public void setResourceLocations(String resourceLocations) {
            this.resourceLocations = resourceLocations;
        }
    }
}
